
from rest_framework import serializers
from django.contrib.auth import get_user_model
from .models import UserProfile

User = get_user_model()


class UserProfileSerializer(serializers.ModelSerializer):
    """用户详细资料序列化器"""
    
    class Meta:
        model = UserProfile
        fields = [
            'id', 'real_name', 'id_card', 'address',
            'driver_license', 'driver_license_image',
            'business_license', 'business_license_image'
        ]
        read_only_fields = ['id']


class UserSerializer(serializers.ModelSerializer):
    """用户序列化器"""
    
    profile = UserProfileSerializer(read_only=True)
    
    class Meta:
        model = User
        fields = [
            'id', 'phone', 'role', 'avatar', 'company',
            'credit_score', 'profile', 'date_joined', 'last_login'
        ]
        read_only_fields = ['id', 'credit_score', 'date_joined', 'last_login']


class UserUpdateSerializer(serializers.ModelSerializer):
    """用户更新序列化器"""
    
    class Meta:
        model = User
        fields = ['phone', 'role', 'avatar', 'company']


class UserProfileUpdateSerializer(serializers.ModelSerializer):
    """用户详细资料更新序列化器"""
    
    class Meta:
        model = UserProfile
        fields = [
            'real_name', 'id_card', 'address',
            'driver_license', 'driver_license_image',
            'business_license', 'business_license_image'
        ]


class ChangePasswordSerializer(serializers.Serializer):
    """修改密码序列化器"""
    
    old_password = serializers.CharField(required=True, help_text='旧密码')
    new_password = serializers.CharField(required=True, help_text='新密码')
    
    class Meta:
        fields = ['old_password', 'new_password']


class ResetPasswordSerializer(serializers.Serializer):
    """重置密码序列化器"""
    
    phone = serializers.CharField(required=True, help_text='手机号')
    code = serializers.CharField(required=True, help_text='验证码')
    new_password = serializers.CharField(required=True, help_text='新密码')
    
    class Meta:
        fields = ['phone', 'code', 'new_password']


class BindPhoneSerializer(serializers.Serializer):
    """绑定手机号序列化器"""
    
    phone = serializers.CharField(required=True, help_text='手机号')
    code = serializers.CharField(required=True, help_text='验证码')
    
    class Meta:
        fields = ['phone', 'code']